CODE_FORM=document.querySelector('#CODE_FORM');
OUTPUT = document.querySelector('output');
function getCode() {
  sel = Array.from(CODE_FORM.querySelectorAll('div,select,input'));
  A = 0
  R = sel.map(v => {
    if (v.id) {
      console.log('-----------------', v.id, v.querySelector('label').innerText)
      return parseInt(v.id, 16).toString(2).padStart(8, '0');
    }
    val = v.value || "0";
    if (v.max && parseInt(val) > parseInt(v.max)) v.value = val = v.max;
    if (v.min && parseInt(val) < parseInt(v.min)) v.value = val = v.min;
    console.log(v.previousElementSibling.innerText, val)

    s = v.getAttribute('datasrc') || '1'
    val = parseInt(val, s.endsWith('b') ? 2 : (s.endsWith('d') ? 10 : 16))
    ss = parseInt(s.trim('bd')) * (s.endsWith('b') ? 1 : 8)
    return val.toString(2).padStart(ss, '0')
  })
  r = R.join('')
  if (r.length % 8) return alert("格式定义错误，字节不完整");
  bin = new Uint8Array(r.length / 8);
  hex = []
  for (i = 0; i < r.length; i += 8) {
    bin[i / 8] = v = parseInt(r.slice(i, i + 8), 2);
    hex.push(v.toString(16).padStart(2, '0'))
  }
  OUTPUT.value = hex.join(' ').toUpperCase();
}


window.onkeyup = getCode;
window.onmouseup = getCode;
// function sel_code(v){
//   console.info(v);
//   $('#code_form').load(`${v}.html`)
// }

